#!/usr/bin/perl

# identify which array was being used from the RPFITs file's
# station locations

use strict;

# which files?
my @rpfits_files;
for (my $i=0;$i<=$#ARGV;$i++){
    if (-e $ARGV[$i]){
	push @rpfits_files,$ARGV[$i];
    }
}

if ($#ARGV<0){
    print "ERROR: no RPFITS files have been specified!\n";
    exit;
}

# The location of our required files.
my $FILEDIR = "/home/jstevens/usr/share";

# now load the coordinates of each station
open(STATIONS,"$FILEDIR/station_coordinates.file");
my @station_x;
my @station_y;
my @station_z;
my @station_name;
while(<STATIONS>){
    chomp;
    my $line=$_;
    $line=~s/^\s*//;
    if ($line=~/^\#/){
	next;
    }
    my @els=split(/\s+/,$line);
    push @station_x,$els[0];
    push @station_y,$els[1];
    push @station_z,$els[2];
    push @station_name,$els[3];
}
close(STATIONS);

# now read in the names of each configuration and their
# corresponding stations
my @configuration_strings;
open(ARRAYS,"$FILEDIR/configuration_stations.file");
while(<ARRAYS>){
    chomp;
    push @configuration_strings,$_;
}
close(ARRAYS);

# ok now go through the RPFITS files and figure out which
# array was used
for (my $i=0;$i<=$#rpfits_files;$i++){
    my $system_command="head -n 1 $rpfits_files[$i] | strings | fold | grep CA0";
    my $got_positions=0;
    my @ant_x;
    my @ant_y;
    my @ant_z;
    for (my $i=1;$i<=6;$i++){
	$ant_x[$i]=-1;
	$ant_y[$i]=-1;
	$ant_z[$i]=-1;
    }
    open(GETPOS,"-|")||exec $system_command;
    while(<GETPOS>){
	chomp;
	my @els=split(/\s+/);
	if (($els[0] eq "")&&
	    ($#els==7)){
	    # probably the right line
	    my $ant_num=$els[2];
	    $ant_num=~s/CA0//;
	    if ($ant_x[$ant_num]!=-1){
		# we're repeating, so we stop
		last;
	    }
	    $ant_x[$ant_num]=$els[4];
	    $ant_y[$ant_num]=$els[5];
	    $ant_z[$ant_num]=$els[6];
	    $got_positions++;
	    if ($got_positions==6){
		last;
	    }
	}
    }
    close(GETPOS);
    my @array_stations;
    # which station the antenna is at can be determined by finding the
    # station which is closest to its coordinates
    for (my $i=1;$i<=6;$i++){
	if ($ant_x[$i]!=-1){
	    my $min_dist=sqrt($ant_x[$i]**2+$ant_y[$i]**2+$ant_z[$i]**2);
	    my $min_station="";
	    for (my $j=0;$j<=$#station_x;$j++){
		my $diff_x=($ant_x[$i]-$station_x[$j]);
		my $diff_y=($ant_y[$i]-$station_y[$j]);
		my $diff_z=($ant_z[$i]-$station_z[$j]);
		my $dist=sqrt($diff_x**2+$diff_y**2+$diff_z**2);
		if ($dist<$min_dist){
		    $min_dist=$dist;
		    $min_station=$station_name[$j];
		}
	    }
	    print "antenna $i is at station $min_station\n";
	    push @array_stations,$min_station;
	}
    }
    
    # which array is now determined by looking for the best match
    my $max_matches=0;
    my $match_array="";
    for (my $i=0;$i<=$#configuration_strings;$i++){
	my $curr_match_count=0;
	for (my $j=0;$j<=$#array_stations;$j++){
	    if ($configuration_strings[$i]=~/$array_stations[$j]/){
		$curr_match_count++;
	    }
	}
	if ($curr_match_count>$max_matches){
	    $max_matches=$curr_match_count;
	    $match_array=$configuration_strings[$i];
	}
    }
    print "best matching array is $match_array\n";
}
    
